#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

struct mouse {
    int w;
    int d;
};
 
bool cmp(mouse x, mouse y) {
    return x.d < y.d;
}

int n;
int sum;
long long ans;
priority_queue<int> q;
mouse a[100005];

int main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i].d;
	}

	for (int i = 0; i < n; ++i) {
		cin >> a[i].w;
	}

	sort(a, a + n, cmp);
	for (int i = 0; i < n; ++i) {
		if (sum < a[i].d) {
			ans += a[i].w;
			++sum;
			q.push(-a[i].w);
		} else {
			if (-q.top() < a[i].w) {
				ans += a[i].w + q.top();
				q.pop();
				q.push(-a[i].w);
			}
		}
	}

	cout << ans << endl;
	return 0;
}	
